package cn.fl.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.product.service.ISplBiApplyRlPsnService;
import cn.fl.product.transform.SplBiApplyRlPsnVOTransform;
import cn.fl.product.transform.SplRlPsnVOTransform;
import cn.fl.product.vo.SplBiApplyRlPsnVO;
import cn.fl.product.vo.SplRlPsnVO;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.api.ISplInfoApplyAddApiService;
import fl.spl.facade.spl.api.ISplInfoApplyApiService;
import fl.spl.facade.spl.api.ISplInfoApplyModifyApiService;
import fl.spl.facade.spl.dto.SplBiApplyRlPsnDTO;

@Service
public class SplBiApplyRlPsnServiceImpl implements ISplBiApplyRlPsnService {

    @Autowired
    private ISplInfoApplyApiService splInfoApplyApiService;

    @Autowired
    private ISplInfoApiService splInfoApiService;

    @Autowired
    private ISplInfoApplyAddApiService splInfoApplyAddApiService;

    @Autowired
    private ISplInfoApplyModifyApiService splInfoApplyModifyApiService;
    
    /**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

    @Override
    public List<SplBiApplyRlPsnVO> getSplRlPsnApplyById(Long id, Long applyId) {
    	
    	
    	List<SplBiApplyRlPsnDTO> splBiApplyRlPsnDTOList = this.splInfoApplyApiService.getSplRlPsnApplyById(id, applyId);
    	//证件类型
        List<KeyValueVO> certTypCdList = this.getKvList(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
        //联系人类型
        List<KeyValueVO> rlTypCDList = this.getKvList(DictionaryConstant.CODE_RL_TYP_CD);
        
        List<SplBiApplyRlPsnVO> splBiApplyRlPsnVOList = SplBiApplyRlPsnVOTransform.toVOList(splBiApplyRlPsnDTOList);
        
    	if(CollectionUtils.isNotEmpty(splBiApplyRlPsnVOList)){
    		for(SplBiApplyRlPsnVO splBiApplyRlPsnVO: splBiApplyRlPsnVOList){
    			String certTypCd = splBiApplyRlPsnVO.getCertTypCd();
    			String certTypCdB = splBiApplyRlPsnVO.getCertTypCdB();
    			String rlTypCd = splBiApplyRlPsnVO.getRlTypCd();
    			String rlTypCdB = splBiApplyRlPsnVO.getRlTypCdB();
    			if(StringUtils.isNotBlank(certTypCd)){
    				for(KeyValueVO keyValueVO: certTypCdList){
    					if(keyValueVO.getKey().equals(certTypCd)){
    						splBiApplyRlPsnVO.setCertTypCdnm(keyValueVO.getValue());
    					}
    				}
    			}
    			if(StringUtils.isNotBlank(certTypCdB)){
    				for(KeyValueVO keyValueVO: certTypCdList){
    					if(keyValueVO.getKey().equals(certTypCdB)){
    						splBiApplyRlPsnVO.setCertTypCdBnm(keyValueVO.getValue());
    					}
    				}
    			}
    			if(StringUtils.isNotBlank(rlTypCd)){
    				for(KeyValueVO keyValueVO: rlTypCDList){
    					if(keyValueVO.getKey().equals(rlTypCd)){
    						splBiApplyRlPsnVO.setRlTypCdnm(keyValueVO.getValue());
    					}
    				}
    			}
    			if(StringUtils.isNotBlank(rlTypCdB)){
    				for(KeyValueVO keyValueVO: rlTypCDList){
    					if(keyValueVO.getKey().equals(rlTypCdB)){
    						splBiApplyRlPsnVO.setRlTypCdBnm(keyValueVO.getValue());
    					}
    				}
    			}
    		}
    	}
        return splBiApplyRlPsnVOList;
    }

    @Override
    public void modifySplBiPsn(List<SplRlPsnVO> vos) throws BaseException {
        if (null != vos && vos.size() > 0) {
            for (SplRlPsnVO vo : vos) {
                //主要联系人，判断id是否存在，存在进行新增，不存在进行修改
                if (StringUtils.isEmpty(vo.getId())) {
                	if(StringUtils.isEmpty(vo.getIsDefault())){
                		vo.setIsDefault(BaseConstant.IS_YESNO_NO);
                	}
                    if (StringUtils.isNotEmpty(vo.getRlTypCd()) && !BaseConstant.IS_YESNO_YES_STR.equals(vo.getRlTypCd())) {
                        Long add = this.splInfoApiService.addPrdSplRlPsn(SplRlPsnVOTransform.toDTO(vo));
                        if (add < 0) {
                            throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
                        }
                    }
                } else {
                    boolean modify = this.splInfoApiService.modifyPrdSplRlPsn(SplRlPsnVOTransform.toDTO(vo));
                    if (!modify) {
                        throw new BaseException(BaseExceptionConstant.COMMON_0002, BaseExceptionConstant.COMMON_0002_NAME);
                    }
                }
            }
        }
    }

    /**
     * 循环更新修改主要联系人信息
     *
     * @param vos
     * @param applyId
     * @throws BaseException
     */
    @Override
    public void saveBiApplyRlPsn(List<SplBiApplyRlPsnVO> vos, Long applyId) throws BaseException {
        if (vos != null) {
            for (SplBiApplyRlPsnVO vo : vos) {
                if (StringUtils.isNotEmpty(vo.getId())) {
                    vo.setSplApplyId(applyId);
                    this.splInfoApplyModifyApiService.modifyBiApplyRlPsn(SplBiApplyRlPsnVOTransform.toDTO(vo));
                } else {
                    vo.setSplApplyId(applyId);
                    Long rtnId = this.splInfoApplyAddApiService.addSplBiApplyRlPsn(SplBiApplyRlPsnVOTransform.toDTO(vo));
                    vo.setId(rtnId);
                }
            }
        }
    }
    
    /**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	public List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
		} catch (Exception e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		return kvList;
	}
}
